<template>
  <div
          v-loading="false"
          element-loading-text="拼命加载中"
          element-loading-spinner="spinner-user"
          element-loading-background="rgba(0, 0, 0, 0)"
          element-loading-custom-class="spinner-custom"
  >
    <div style="display:flex;flex-wrap:wrap" v-show="showSVNButton">
      <div style="display:flex;">
        <el-button
          type="normal"
          @click="handleRefresh(true)"
          class="svn-button"
        >
          <svg-icon icon-class="svn" class="svn-button-icon"/><span>状态</span>
        </el-button>
        <el-button
          type="normal"
          @click="handleAdd"
          :disabled="btnDisable.add"
          v-show="propShowAdd"
          class="svn-button"
        >+SVN</el-button>
        <el-button
          type="normal"
          @click="handleLock"
          :disabled="btnDisable.lock"
          class="svn-button"
        >加锁</el-button>
        <el-button
          type="normal"
          @click="handleCommit"
          :disabled="btnDisable.commit"
          class="svn-button"
        >提交</el-button>
        <el-button
          type="normal"
          @click="handleRevert"
          :disabled="btnDisable.revert"
          class="svn-button"
        >撤销</el-button>
        <el-button
          type="normal"
          @click="handleDelete"
          :disabled="btnDisable.delete"
          v-show="propShowDelete"
          class="svn-button"
        >删除</el-button>
      </div>
      <div style="display:flex;margin-left:20px;">
        <div :class="cpLockStatusClass">
          <svg-icon :icon-class="cpLockStatusIconClass" class="svn-status-icon"/>{{svnLockInfo.lockStatus}}
        </div>
        <div :class="cpLockStatusClass">
          <svg-icon icon-class="user-solid" class="svn-status-icon"/>{{svnLockInfo.userName}}
        </div>
        <div :class="cpLockStatusClass">
          <svg-icon icon-class="station" class="svn-status-icon"/>{{svnLockInfo.station}}
        </div>
      </div>      
    </div>
    <div style="height: 19px;color: red;margin-top: 10px;" v-show="showSVNButton">未锁定或者非本机锁定状态时，不可编辑</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      svnLockInfo: {
        lockStatus: 'UNKNOWN',
        userName: '--',
        station: '--'
      },
      btnDisable: {
        add: true,
        lock: true,
        commit: true,
        revert: true,
        delete: true
      },
      showSVNButton: true,
      fileName: '',
      apiSVNStatus: '/program/svn/status',
      apiSVNAdd: '/program/svn/add',
      apiSVNLock: '/program/svn/lock',
      apiSVNCommit: '/program/svn/commit',
      apiSVNRevert: '/program/svn/revert',
      apiSVNDelete: '/program/svn/delete',
      listLoading: false
    }
  },
  props: {
    propSourceType: String,
    propFileName: String,
    propShowAdd: Boolean,
    propShowDelete: Boolean
  },
  created() {
  },
  computed: {
    cpLockStatusClass() {
      const divClassArray = ['svn-status-container', 'svn-status-width']
      if (this.svnLockInfo.lockStatus == 'LOCK') {
        divClassArray.push('grey')
      } else if (this.svnLockInfo.lockStatus == 'LOCK_BY_SELF') {
        divClassArray.push('green')
      } else if (this.svnLockInfo.lockStatus == 'UNKNOWN') {
        divClassArray.push('red')
      }
      return divClassArray
    },
    cpLockStatusIconClass() {
      return ['LOCK_BY_SELF', 'DRAFT', 'ADDED'].includes(this.svnLockInfo.lockStatus) ? 'unlock-solid' : 'lock-solid'
    }
  },
  methods: {
    // 外部调用函数
    ocSetFileName(fileName, flagAsync = false, noLoad) {
      this.fileName = fileName || ''
      this.handleRefresh(flagAsync, noLoad)
    },
    setButtonState() {
      // 不显示SVN
      if (this.svnLockInfo.lockStatus == 'NO_SVN') {
        this.showSVNButton = false
      // 未锁定状态
      } else if (this.svnLockInfo.lockStatus == 'FREE') {
        this.$set(this.btnDisable, 'add', true)
        this.$set(this.btnDisable, 'lock', false)
        this.$set(this.btnDisable, 'commit', true)
        this.$set(this.btnDisable, 'revert', true)
        this.$set(this.btnDisable, 'delete', true)
      // 被自己锁定状态
      } else if (this.svnLockInfo.lockStatus == 'LOCK_BY_SELF') {
        this.$set(this.btnDisable, 'add', true)
        this.$set(this.btnDisable, 'lock', true)
        this.$set(this.btnDisable, 'commit', false)
        this.$set(this.btnDisable, 'revert', false)
        this.$set(this.btnDisable, 'delete', false)
      // 新增状态
      } else if (this.svnLockInfo.lockStatus == 'ADDED') {
        this.$set(this.btnDisable, 'add', true)
        this.$set(this.btnDisable, 'lock', true)
        this.$set(this.btnDisable, 'commit', false)
        this.$set(this.btnDisable, 'revert', false)
        this.$set(this.btnDisable, 'delete', true)
        // 草稿状态
      } else if (this.svnLockInfo.lockStatus == 'DRAFT') {
        this.$set(this.btnDisable, 'add', false)
        this.$set(this.btnDisable, 'lock', true)
        this.$set(this.btnDisable, 'commit', true)
        this.$set(this.btnDisable, 'revert', true)
        this.$set(this.btnDisable, 'delete', true)
      // 被他人锁定或其他未判断状态，全部锁定
      } else {
        this.$set(this.btnDisable, 'add', true)
        this.$set(this.btnDisable, 'lock', true)
        this.$set(this.btnDisable, 'commit', true)
        this.$set(this.btnDisable, 'revert', true)
        this.$set(this.btnDisable, 'delete', true)
      }
    },
    setLockStatus(data) {
      this.$set(this.svnLockInfo, 'lockStatus', data.lockStatus)
      this.$set(this.svnLockInfo, 'userName', data.userName)
      this.$set(this.svnLockInfo, 'station', data.station)
      this.setButtonState()
      // callback
      this.$emit('lock-status-change', this.svnLockInfo)
    },
    async doSVNAction(api, remark2, flagAsync, noLoad) {
      if (!this.showSVNButton) {
        return
      }
      const params = {
        sourceType: this.propSourceType,
        fileName: this.fileName,
        remark: remark2
      }
      // this.$emit('set-loading', true)
      if (flagAsync && flagAsync == true) {
        if (!(noLoad && noLoad == true)) this.utilVue.loadShow(true)
        this.$ajax.get(api, { params }).then(res => {
          if (res.code == 0) {
            this.setLockStatus(res.data || {})
            // this.utilMessage.success(res.msg)
          }
          this.utilVue.loadHide()
          // this.$emit('set-loares.data', false)
        }).catch(err => {
          this.utilMessage.error(err)
          // this.$emit('set-loading', false)
        })
      } else {
        this.listLoading = true
        await this.$ajax.get(api, { params }).then(res => {
          if (res.code == 0) {
            this.setLockStatus(res.data || {})
            this.utilMessage.success(res.msg)
          }
          this.listLoading = false
          // this.$emit('set-loares.data', false)
        }).catch(err => {
          this.utilMessage.error(err)
          // this.$emit('set-loading', false)
        })
      }
    },
    handleRefresh(flagAsync, noLoad) {
      this.doSVNAction(this.apiSVNStatus, '', flagAsync, noLoad)
    },
    handleAdd() {
      this.doSVNAction(this.apiSVNAdd)
    },
    handleLock() {
      this.doSVNAction(this.apiSVNLock, '', true)
    },
    async handleRefreshOut(fileName) {
      this.fileName = fileName
      await this.doSVNAction(this.apiSVNStatus)
    },
    async handleCommit() {
      // const result = await this.$confirm('所有的已经修改的内容将全部提交到SVN，继续操作？', '数据提交警告')
      // if (!result) {
      //   return
      // }

      this.$prompt('请输入提交说明（必填）', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /.+/,
        type: 'warning',
        inputType: 'textarea',
        closeOnClickModal: false,
        inputErrorMessage: '请输入提交说明'
      }).then(({ value }) => {
        this.doSVNAction(this.apiSVNCommit, value, true)
      }).catch(() => {
      })
    },
    async handleRevert() {
      const result = await this.$confirm('所有的已经修改的内容将全部丢失，恢复到你上次提交的版本，继续操作？', '数据回滚警告')
      if (!result) {
        return
      }
      this.doSVNAction(this.apiSVNRevert, '', true)
    },
    async handleDelete() {
      const result = await this.$confirm('确定要删除文件并提交，继续操作？', '数据删除警告')
      if (!result) {
        return
      }
      this.doSVNAction(this.apiSVNDelete, '',  true)
    }
  }
}
</script>
<style lang="scss" scoped>
  .svn-status-container {
     padding:5px;
     font-size:18px;
  }
  .svn-status-icon {
     margin-right:5px;
     font-weight: bold;
     font-size:20px;
  }
  .svn-button {
    width:75px;
  }
  .svn-button-icon {
     margin-right:5px;
     font-weight: bold;
     font-size:16px;
  }
  .svn-status-width {
    min-width:150px;
    max-width:300px;
  }
  .green {
    color:darkgreen;
  }
  .grey {
    color:grey;
  }
  .red {
    color:red;
  }
</style>
